﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Tetris
{
    class Painter
    {
        public char frameChar;
        public char NodeSymbol { get; private set; }

        public Painter(char frame, char symbol)
        {
            frameChar = frame;
            NodeSymbol = symbol;
        }

        public void DrawGameFieldFrame(GameField field)
        {
            int positionY = field.StartY - 1;
            for (int i = 0; i < field.height + 1; i++)
            {
                Console.SetCursorPosition(field.StartX - 1, positionY);
                for (int j = 0; j < field.width + 2; j++)
                {
                    if ((i == 0 && (j == 0 || j == field.width + 1)) || i == field.height)
                    {
                        Console.Write(frameChar);
                    }
                    else if (j == 0 || j == field.width + 1)
                    {
                        Console.Write(frameChar);
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }
                Console.WriteLine();
                Console.SetCursorPosition(field.StartX, ++positionY);
            }
        }

        public void DrawFigure(Figure figure, GameField field)
        {
            Console.ForegroundColor = figure.Node1.NodeColor;
            if (CheckList.IsReadyToDraw(figure.Node1, field))
                Console.WriteLine(NodeSymbol);
            if (CheckList.IsReadyToDraw(figure.Node2, field))
                Console.WriteLine(NodeSymbol);
            if (CheckList.IsReadyToDraw(figure.Node3, field))
                Console.WriteLine(NodeSymbol);
            if (CheckList.IsReadyToDraw(figure.Node4, field))
                Console.WriteLine(NodeSymbol);
            Thread.Sleep(10);
            Console.ForegroundColor = ConsoleColor.White;
        }

        public void ClearFigure(Figure figure)
        {
            Console.SetCursorPosition(figure.Node1.X, figure.Node1.Y);
            Console.WriteLine(' ');
            Console.SetCursorPosition(figure.Node2.X, figure.Node2.Y);
            Console.WriteLine(' ');
            Console.SetCursorPosition(figure.Node3.X, figure.Node3.Y);
            Console.WriteLine(' ');
            Console.SetCursorPosition(figure.Node4.X, figure.Node4.Y);
            Console.WriteLine(' ');
        }

        public void DrawNodesOnFloor(List<Node> nodeList)
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var node in nodeList)
            {
                Console.SetCursorPosition(node.X, node.Y);
                Console.WriteLine(NodeSymbol);
            }
            Console.ForegroundColor = ConsoleColor.White;
        }

        public void DrawScoreAndNextFigure(GameField field, int shift, int score, int speed, Figure figure)
        {
            Console.ForegroundColor = figure.Node1.NodeColor;
            Console.SetCursorPosition(figure.Node1.X + 11, figure.Node1.Y + 4);
            Console.WriteLine(NodeSymbol);
            Console.SetCursorPosition(figure.Node2.X + 11, figure.Node2.Y + 4);
            Console.WriteLine(NodeSymbol);
            Console.SetCursorPosition(figure.Node3.X + 11, figure.Node3.Y + 4);
            Console.WriteLine(NodeSymbol);
            Console.SetCursorPosition(figure.Node4.X + 11, figure.Node4.Y + 4);
            Console.WriteLine(NodeSymbol);
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(field.StartX + field.width + 1 + shift, field.StartY + 6);
            Console.WriteLine("Round: {0}", speed);
            Console.SetCursorPosition(field.StartX + field.width + 1 + shift, field.StartY + 7);
            Console.Write("Score: {0}", score);

        }
    }
}
